package com.inhim.pjhim.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.entity.ReaderTypeList;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;



/**
 * Created by hsmacmini on 2018/4/8.
 */

public class HorizontalRecViewAdapter extends RecyclerArrayAdapter<ReaderTypeList.List> {
    private Context mContext;
    final static int ITEMS_PER_PAGE = 5;
    private int temp;
    private ItemOnClickListener mItemOnClickListener;
    public HorizontalRecViewAdapter(Context context ,int adapterPsition){
        super(context);
        this.mContext=context;
        temp=adapterPsition;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        final ViewHolder holder;
        View view = null;
        if (view == null) {
            view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal_rec_view,parent,false);
            holder = new ViewHolder(view,viewType);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                temp = viewType;
                //刷新界面 notify 通知Data 数据set设置Changed变化
                //在这里运行notifyDataSetChanged 会导致下面的onBindViewHolder 重新加载一遍
                holder.tv_name.setTextSize(16);
                holder.tv_name.setTextColor(mContext.getResources().getColor(R.color.baseColor));
                holder.und_line.setVisibility(View.VISIBLE);
                notifyDataSetChanged();
                mItemOnClickListener.onClick(viewType);
            }
        });
        return holder;
    }

    public void notfyPosition(int adapterPsition){
        temp=adapterPsition;
        notifyDataSetChanged();
    }

    class ViewHolder extends BaseViewHolder<ReaderTypeList.List>{
        TextView tv_name;
        View und_line;
        LinearLayout constran;
        public ViewHolder(@NonNull View itemView,int position) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            und_line=itemView.findViewById(R.id.und_line);
            constran=itemView.findViewById(R.id.constran);
        }

        @Override
        public void setData(ReaderTypeList.List data) {
            super.setData(data);
            //动态计算屏幕宽度 一行显示四个item
            Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mTextPaint.setTextSize(mContext.getResources().getDimensionPixelSize(R.dimen.text_size_15));
            int textWidth = (int) mTextPaint.measureText(data.getName());
            int margin=mContext.getResources().getDimensionPixelSize(R.dimen.base_dimen_30);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((textWidth+margin),

                    ViewGroup.LayoutParams.MATCH_PARENT);
            constran.setLayoutParams(params);
            tv_name.setText(data.getName());
            if(temp==getAdapterPosition()){
                tv_name.setTextSize(16);
                tv_name.setTextColor(mContext.getResources().getColor(R.color.baseColor));
                und_line.setVisibility(View.VISIBLE);
            }else{
                tv_name.setTextSize(15);
                tv_name.setTextColor(mContext.getResources().getColor(R.color.color_555));
                und_line.setVisibility(View.INVISIBLE);
            }
        }
    }

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener){
        mItemOnClickListener=itemOnClickListener;
    }
    public interface ItemOnClickListener{
        void onClick(int position);
    }
}
